package engine.newUi
{
	import engine.control.loading.MultLoader;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.data.RoleVo;
	import engine.display.Optimize;
	import engine.flag.ResourcesPath;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.tip.tipItem.StrideFighterItem;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.system.Capabilities;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.utils.MemoryOptimise;
	
	/**
	 *跨服战主界面 
	 * @author yanghr
	 * 
	 */
	public class StrideMainLogic
	{
		/**
		 *天邦 
		 */		
		private const SKY:String="skyTop";
		/**
		 *地榜 
		 */		
		private const GROUND:String="bottomTop";
		/**
		 *规则按钮 
		 */		
		private const RULE_BTN:String="top_btn1";
		/**
		 *下注按钮 
		 */		
		private const CHIP_BTN:String="top_btn2";
		/**
		 *战绩 按钮
		 */		
		private const WARFARE_BTN:String="top_btn3";
		/**
		 *身价 按钮 
		 */		
		private const VALUE_BTN:String="top_btn4";
		/**
		 *人物	按钮 
		 */		
		private const ACTOR_BTN:String="bottom_btn1";
		/**
		 *背包 按钮 
		 */		
		private const PACK_BTN:String="bottom_btn2";
		/**
		 * 布阵	按钮
		 */		
		private const EMBATTLE_BTN:String="bottom_btn3";
		/**
		 *内修 	按钮
		 */		
		private const INTER_BTN:String=	"bottom_btn4"
		/**
		 *强化 按钮 
		 */			
		private const STRENGTH_BTN:String="bottom_btn5"
		/**
		 *经脉	按钮 
		 */			
		private const SKILL_BTN:String="bottom_btn6";
		/**
		 *心法	按钮 
		 */		
		private const KUNGFU_BTN:String="bottom_btn7";
		/**
		 *关闭按钮 
		 */		
		private const CLOSE_BTN:String="closeBtn";
		/**
		 *当前第几轮文本 
		 */		
		private const CENTER_TIME_TXT:String="timetxt";
		/**
		 *当前时间 
		 */		
		private const NOW_TIME_TXT:String="nowtimeText";
		/**
		 *铜币文本 
		 */		
		private const MONEY_TXT:String="moneyText";
		/**
		 *元宝文本 
		 */		
		private const GOLD_TXT:String="goldText";
		/**
		 *时间线 
		 */		
		private const TIME_MC:String="timeline";
		/**
		 *比赛时间 
		 */		
		private const ATTACK_TIME:String="attack_time";
		/**头像**/
		private const CHAMPION_ICON:String = "championIcon";
		
		private const ICON:String="icon_";
		
		/**
		 *铜钱 
		 */		
		private var gold:int=0;
		/**
		 *元宝 
		 */		
		private var magic:int=0;
		/**
		 * SKY_LOCAL	本地天邦
		 * GROUND_LOCAL	本地地榜
		 * SKY_SERVER	跨服天邦
		 * GROUND_SERVER	跨服地榜
		 */		
		public static const SKY_LOCAL:int=8,GROUND_LOCAL:int=9,SKY_SERVER:int=15,GROUND_SERVER:int=16;
		private static var _self:StrideMainLogic;
		public static function getInstance():StrideMainLogic
		{
			if(_self==null)
			{
				_self=new StrideMainLogic();
			}
			return _self;
		}
		private var _ui:StrideMainUI;
		public function get ui():StrideMainUI
		{
			if(_ui==null)
			{
				_ui=new StrideMainUI;
				Init();
			}
			return _ui;
		}
		public function StrideMainLogic()
		{
		}
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		
		private var _item:StrideFighterItem;
		
		private var _mainMc:MovieClip;
		/**
		 *天邦数据 (0没用)
		 */		
		private const _skyGroup:Vector.<BattleNode>=new Vector.<BattleNode>(32);
		/**
		 *地榜数据 (0没用)
		 */		
		private const _groundGroup:Vector.<BattleNode>=new Vector.<BattleNode>(32);
		/**
		 *储存所有玩家 
		 */		
		private const _playerInfoMap:Object={};
		
		private var _initCallBack:Array=[];
		/**
		 *是否是本地战； 
		 */		
		private var _isLocal:Boolean=false;
		/**
		 *是不是天邦 
		 */		
		private var _isSky:Boolean=true;
		
		private var _date:Date;
		private var _Time:Timer;
		public function get currentType():int
		{
			if(_isLocal)
			{
				if(_isSky)
				{
					return SKY_LOCAL
				}
				else{
					return GROUND_LOCAL;
				}
			}
			else
			{
				if(_isSky)
				{
					return SKY_SERVER
				}
				else{
					return GROUND_SERVER;
				}
			}
			
		}
		public function  Init():void
		{
			_date =new Date();
			_Time = new Timer(1000);
			ui;
			_bind=BindManage.getInstance();
			_item=new StrideFighterItem;
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			ui.initPosY=-55;
			initBG();
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_RISE_TIME_STATUS),changeTime);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_RISE_SITE_INFO),changeBattle);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_RISE_FIGHT_PLAYER),changeNames);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_SHOW_PROP),changeInfo);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_UPDATE_PLEDGE),changeIconState);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_REFRESH_UI),changeUI);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SENDSERVERTIME), getServerTime);
		}
		private function changeUI(e:CustomMsgEvent):void
		{
			trace("StrideMainLogic.changeUI(e)",e.pList,_ui.created||StrideWarfareLogic.getInstance().ui.created,_ui.visible);
			if(_ui.visible||StrideWarfareLogic.getInstance().ui.created)
			{
				//刷新界面
				trace("StrideMainLogic.changeUI(e)“发送消息”");
				
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ON_BTN_ICON);
			}
		}
		
		private function initBG():void
		{
			_initCallBack.push(initafterLodedAll);
			//			_initCallBack.push(BeforeShow);
			var len:int=_skyGroup.length;
			for (var j:int = 0; j < len; j++) 
			{
				_skyGroup[j]=new BattleNode();
				_skyGroup[j].init(_skyGroup);
			}
			len=_groundGroup.length;
			for ( j = 0; j < len; j++) 
			{
				_groundGroup[j]=new BattleNode();
				_groundGroup[j].init(_groundGroup);
			}
			const url:String=ResPath.SWF + "strideUI.swf";
			MultLoader.getInstance().load([url],false,function ():void{
				_mainMc=MultLoader.getInstance().getSourceByUrl(url)["mc"];
				_mainMc[CHAMPION_ICON].mouseChildren = false;
				var text:TextField = _mainMc["icon_31"]["timetxt"] as TextField;
				text.htmlText=StaticIni.getSwfLangStr("Stride_15");
				_mainMc["icon_31"] .buttonMode = true;
				_ui.addChild(_mainMc);
				//				Optimize.stopMovieClip(_mainMc);
				MultLoader.getInstance().deletAllSource();
				callBackALl();
				MemoryOptimise.stopMovie(_mainMc);
			});
			
		}
		private function callBackALl():void
		{
			for each (var i:Function in _initCallBack) 
			{
				i();
			}
			_initCallBack=null;
			_ui.visible=true;
		}
		private var _isFirstInit:Boolean=true;
		public function	BeforeShow():void
		{
			if(!checkInitFinish){
				_ui.visible=false;
				return ;
			}
			addAllEventListener();
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_QUERYSERVERTIME);
		}
		private function getServerTime(e:CustomMsgEvent):void
		{
			var serverTime:int	=	e.pList[0];
			_date = new Date(serverTime*1000);
			_Time.start();
		}
		private function playTime(e:TimerEvent):void
		{
			_date.time-=1000;
			_date = new Date(_date.time);
			if(_date.time>0)
			{
				_mainMc[NOW_TIME_TXT].htmlText = MysterySellLogic.getInstance().getTimeStr(_date);
			}
			else
			{
				_mainMc[NOW_TIME_TXT].htmlText ="00:00:00";
				_Time.reset();
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SERVER_TIME);
			}
		}
		private function initNames(group:int=SKY_LOCAL):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_RISE_PLAYER,group)
		}
		private function initBattle(group:int=GROUND_LOCAL):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_SITE_WINPLAYER,group)
		}
		public function	AfterHide():void
		{
			removeAllEventListener()
		}
		private var _bindMoney:int=-1;
		private var _bindGold:int=-1;
		public function addAllEventListener():void
		{
			_Time.addEventListener(TimerEvent.TIMER,playTime);
			_mainMc.addEventListener(MouseEvent.CLICK,mainClickHandle);
			_mainMc.addEventListener(MouseEvent.MOUSE_OVER,mainRollhandle);
			_mainMc.addEventListener(MouseEvent.MOUSE_OUT,mainOuthandle);
			if( _bindMoney == -1 )
			{
				_bindMoney		=_bind.BindProp(_role,"GoldCoin",this,"goldCoin")
				
			}
			if(_bindGold	==	-1)
			{
				_bindGold	 = _bind.BindProp(_role,"MagicCoin",this,"magicCoin")
			}
			
		}
		private function mainRollhandle(e:Event):void
		{
			//			trace("StrideMainLogic.mainRollhandle(e)",e.currentTarget.name,e.target.name);
			var mc:MovieClip;
			switch(e.target.name)
			{
				case SKY:
				case GROUND:
				{
					mc	=	e.target as MovieClip;
					if(mc.currentFrame!=3)
						mc.gotoAndStop(2);
					break;
				}
				case MONEY_TXT:
					_tip.showString(StaticIni.getSwfLangStrVar("PeeragePrize_1",[gold]));
					break;
				case GOLD_TXT:
					_tip.showString(StaticIni.getSwfLangStrVar("PeeragePrize_2",[magic]));
					break;
				
				default:
				{
					if(e.target is TextField)
					{
						const TEXT:String="txt_"
						var	name:String=e.target.name;
						if(name.indexOf(TEXT)>-1)
						{
							//							var	index:int=name.split(TEXT)[1];
							rollname(e.target.text);
						}
						
					}
					else if(e.target is SimpleButton)
					{
						mc=e.target.parent as MovieClip;
						if(mc.currentFrame==3)//已下注
						{
							_tip.showMain(StrideChipLogLogic.getInstance().getCurrentTipsStr());
						}
						
					}
					break;
				}
			}
		}
		private function rollname(name:String):void
		{
			//			trace("StrideMainLogic.rollname(name)",name);
			if(name)
			{
				var obj:Object=_playerInfoMap[name];
				if(obj)
				{
					_item.data=obj
					_tip.showByItem(_item);
				}
			}
			
		}
		private function mainOuthandle(e:Event):void
		{
			//			trace("StrideMainLogic.mainOuthandle(e)",e.currentTarget.name,e.target.name);
			var mc:MovieClip;
			switch(e.target.name)
			{
				case SKY:
				case GROUND:
				{
					mc	=	e.target as MovieClip;
					if(mc.currentFrame==2)
						mc.gotoAndStop(1);
					break;
				}
					
				default:
				{
					_tip.hide();
					break;
				}
			}
		}
		private function mainClickHandle(e:Event):void
		{
			//			trace("StrideMainLogic.mainClickHandle(e)",e.currentTarget.name,e.target.name);
			var name:String=e.target.name;
			switch(name)
			{
				case SKY:
				{
					if(currentGroup!=_skyGroup)
						currentGroup=_skyGroup
					break;
				}
				case GROUND:
				{
					if(currentGroup != _groundGroup)
						currentGroup=_groundGroup;
					break;
				}
				case ACTOR_BTN:
				{
					MyPropertyLogic.getInstance().ui.showOrHide();
					break;
				}
				case PACK_BTN:
				{
					PackLogic.getInstance().ui.showOrHide();
					break;
				}
				case CLOSE_BTN:
				{
					close(null);
					break;
				}
				case SKILL_BTN:
				{
					PulseLogic.getInstance().ui.show();
					break;
				}
				case INTER_BTN:
				{
					StatusShopUI.getInstance().showOrHide();
					break;
				}
				case STRENGTH_BTN:
				{
					EquipModifiedUI.getInstance().showOrHide();
					break;
				}
				case EMBATTLE_BTN:
				{
					EmbattleLogic.getInstance().ui.showOrHide();
					break;
				}
				case KUNGFU_BTN:
				{
					KungfuPackLogic.getInstance().ui.showOrHide();
					break;
				}
				case RULE_BTN:
				{
					StrideRuleUI.getInstance().showOrHide()
					break;
				}
				case CHIP_BTN:
				{
					//					StrideChipInLogic.getInstance().ui.showOrHide();
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_MY_PLEDGE);
					break;
				}
				case WARFARE_BTN:
				{
					//					StrideWarfareLogic.getInstance().ui.showOrHide()
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_RISE_MY_RACE);
					
					break;
				}
				case VALUE_BTN:
				{
					StridePerValueLogic.getInstance().ui.showOrHide();
					break;
				}
				case CHAMPION_ICON:
				{
					clickIconhandle(31);
					break;
				}
				default:
				{
					if(e.target is SimpleButton)
					{
						name=e.target.parent.name
						if(name.indexOf(ICON)>-1)
						{
							var index:int=name.split(ICON)[1];
							trace("StrideMainLogic.mainClickHandle(e)当前点击的是xxxxx"+index);
							clickIconhandle(index)
							
						}
					}
					else if(e.target is TextField)
					{
						if(_mainMc[ICON+31].contains(e.target as DisplayObject))
						{
							clickIconhandle(31)
						}
					}
					break;
				}
			}
		}
		/**
		 *点击当前的icon_ 
		 * @param index
		 * 
		 */		
		private function clickIconhandle(index:int):void
		{
			var icon:MovieClip=_mainMc[ICON+index];
			if(icon.currentFrame==1)
			{
				//查看战报
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_SITE_INFO,currentType,index);	
			}
			else if(icon.currentFrame==2)
			{
				//下注
				var battle:BattleNode=_currentGroup[index];
				var per1:Object=_playerInfoMap[battle.t_Name];
				var per2:Object=_playerInfoMap[battle.b_Name];
				if(per1&&per2)
				{
					trace("Right~StrideMainLogic.clickIconhandle(index)",battle);
					StrideChipInLogic.getInstance().showWithPer(per1,per2,_round,currentType,index);
				}
				else
				{
					trace("Error~StrideMainLogic.clickIconhandle(index)",battle);
				}
			}
		}
		public function removeAllEventListener():void
		{
			_Time.removeEventListener(TimerEvent.TIMER,playTime);
			_mainMc.removeEventListener(MouseEvent.CLICK,mainClickHandle);
			_bindMoney		=	_bind.UnbindProp(_role,"GoldCoin",_bindMoney)
			_bindGold		=	_bind.UnbindProp(_role,"MagicCoin",_bindGold)
			
		}
		private function close(e:Event):void
		{
			_ui.hide();
		}
		/**
		 *fightType, site, 下注状态  
		 * @param e
		 * 
		 */		
		private function changeIconState(e:CustomMsgEvent):void
		{
			var arr:Array=e.pList;
			var type:int=arr[0];
			var index:int=arr[1];
			var state:int=arr[2];
			var group:Vector.<BattleNode>=getGroupByType(type);
			group[index].isSee=state;
			if(checkTypeIsCurrent(type))
			{
				refreshIndexBattle(index)
			}
		}
		/**
		 *玩家名
		 等级
		 声望
		 战力
		 职业
		 服装号
		 宠物等级
		 宠物id
		 伙伴数量
		 数组 伙伴模型号 
		 * @param e
		 * 
		 */		
		private function changeInfo(e:CustomMsgEvent):void
		{
			trace("StrideMainLogic.changeInfo(e)",e.pList);
			var strideper:StridePerValueLogic=	StridePerValueLogic.getInstance();
			//			strideper.clearData();
			var arr:Array=e.pList.concat();
			var obj:Object;
			var target:Object;
			while(arr.length)
			{
				var name:String=arr.shift();
				obj=	_playerInfoMap[name]={};
				obj[ActorPropertyUI.NAME]=name;
				obj[ActorPropertyUI.LEVEL]=arr.shift();
				obj[ActorPropertyUI.REPUTATION]=arr.shift();
				obj[ActorPropertyUI.FIGHT_POWER]=arr.shift();
				obj[ActorPropertyUI.MODEL]=arr.shift();
				obj[ActorPropertyUI.CLOTHE_MODEL]=arr.shift();
				obj["petId"]=arr.shift();
				obj["petLevel"]=arr.shift();
				obj[ActorPropertyUI.PER_VALUE]=arr.shift();
				var len:int=arr.shift();
				for (var i:int = 1; i <= len	; i++) 
				{
					obj["partner"+i]=arr.shift();
				}
				//添加到身价界面表里
				strideper.addPerson(obj);
			}
			
		}
		private function changeNames(e:CustomMsgEvent):void
		{
			trace("StrideMainLogic.changeNames(e)",e.pList);
			var type:int=e.pList[0];
			var len:int=e.pList.length-1;
			var group:Vector.<BattleNode>=getGroupByType(type);
			for (var i:int = 0; i < len	; i+=3) 
			{
				var index:int=e.pList[1+i];
				var tp:String=e.pList[2+i];
				var bp:String=e.pList[3+i]
				//				group[index].init(group)
				group[index].initPlayer(tp,bp);
			}
			//			refreshAll();
			if(checkTypeIsCurrent(type))
			{
				refreshNames()
				refreshAllBattle();
			}
		}
		/**
		 *检查当前type是不是当前显示的 
		 * @param type
		 * @return 
		 * 
		 */		
		private function checkTypeIsCurrent(type:int):Boolean
		{
			return ((type==SKY_LOCAL||type==SKY_SERVER)&&currentGroup==_skyGroup)||((type==GROUND_LOCAL||type==GROUND_SERVER)&&currentGroup==_groundGroup);
		}
		private function refreshNames():void
		{
			for (var i:int = 1; i <=16; i++) 
			{
				var batt:BattleNode=currentGroup[i];
				_mainMc["txt_"+(i*2-1)].text=batt.b_Name;
				_mainMc["txt_"+(i*2-2)].text=batt.t_Name;
			}
			
		}
		private function changeBattle(e:CustomMsgEvent):void
		{
			trace("StrideMainLogic.changeBattle(e)",e.pList);
			if(!checkInitFinish)
			{
				_initCallBack.push(function():void{changeBattle(e)});
				if(!ui.created)
				{
					_ui.showOrHide();
				}
				return ;
			}
			var type:int=e.pList[0];
			_isLocal=type==SKY_LOCAL||type==GROUND_LOCAL;
			if(_isFirstInit)
			{
				initNames(currentType);
				_isFirstInit=false;
			}
			var len:int=e.pList.length-1;
			var group:Vector.<BattleNode>=getGroupByType(type);
			for (var i:int = 0; i < len	; i+=3) 
			{
				var index:int=e.pList[1+i];
				var result:String=e.pList[2+i];
				var isShowState:int=e.pList[3+i]
				//				group[index].init(group)
				group[index].initBattle(result,isShowState);
				if(checkTypeIsCurrent(type))
					refreshIndexBattle(index)
			}
			if(!ui.created)
			{
				_ui.showOrHide();
			}
			//			refreshAllBattle();
		}
		/**
		 *将场次 转化为轮数 
		 * @param index
		 * @return 
		 * 
		 */		
		public function getTurnFromIndex(index:int):int
		{
			return _skyGroup[index].turnCount;
		}
		private function changeTime(e:CustomMsgEvent):void
		{
			trace("StrideMainLogic.changeTime(e)",e.pList);
			_round=(int(e.pList[0]));
			var _time:int=e.pList[1];
			timeLine=_time+1;
			//			timeTxt=_round;
			setTurnInfo(_round,_time);
			attacktimeMc=_round;
		}
		private function set attacktimeMc(turn:int):void
		{
			_mainMc[ATTACK_TIME].gotoAndStop(turn==5?2:1);
		}
		/**
		 *设置比赛预报 
		 * @param turn
		 * @param wheel
		 * 
		 */		
		private function setTurnInfo(turn:int,wheel:int):void
		{
			var Str:String;
			
			if(wheel==5) //预报明天
			{
				
				Str ="Stride_17";
				turn==0?1:turn;
				turn=turn+1;
			}
			else   //预报今天
			{
				Str ="Stride_16";
				if(turn==0)  //准备状态
				{
					turn=1;
				}
			}
			
			var type:String = _isLocal?"local_":"stride_";
			var nextTurn:int=((turn)%5==0)?5:(turn)%5;
			var nextWheel:int=((wheel+1)%5==0)?5:(wheel+1)%5
			var attackWheel:String=	StaticIni.getSwfLangStr("StrideRound_"+(nextTurn+(_isSky?0:5)))+StaticIni.getSwfLangStrVar("UIString_1000594",[nextWheel])//"第"+nxetWheel+"场"  //18强赛 
			var time:String = StaticIni.getIniVar("group_"+turn,type+nextWheel);
			_mainMc[CENTER_TIME_TXT].htmlText=StaticIni.getSwfLangStrVar(Str,[time,attackWheel]);
		}
		private var _time:int
		private var _round:int;
		private function refreshTime():void
		{
			timeLine=_time;
		}
		/**
		 *时间线 
		 * @param val
		 * 
		 */		
		private function set timeLine(val:int):void
		{
			if(_mainMc)
				_mainMc[TIME_MC].gotoAndStop(val);
			else
				_initCallBack.push(function():void{timeLine=val})
		}
		private function set timeTxt(val:int):void
		{
			val =val==0?1:val;
			var str:String=	StaticIni.getSwfLangStr("StrideRound_"+(val+(_isSky?0:5)))//val+"";
			_mainMc[CENTER_TIME_TXT].htmlText=StaticIni.getSwfLangStrVar("Stride_11",[str]);
		}
		private function getGroupByType(type:int):Vector.<BattleNode>
		{
			return (type==SKY_LOCAL||type==SKY_SERVER)?_skyGroup:_groundGroup;;
		}
		private var _currentGroup:Vector.<BattleNode>;
		/**
		 *当前榜单 
		 * @return 
		 * 
		 */		
		private function get currentGroup():Vector.<BattleNode>
		{
			return _currentGroup;
		}
		private var _isfirstGround:Boolean=true;
		private function set currentGroup(val:Vector.<BattleNode>):void
		{
			_currentGroup=val;
			//显示
			_mainMc[SKY].gotoAndStop(val==_skyGroup?3:1);
			_mainMc[GROUND].gotoAndStop(val==_skyGroup?1:3);
			if( _mainMc["championIcon"]["mc"])
				_mainMc["championIcon"]["mc"].gotoAndStop(val==_skyGroup?1:2);
			//			_isSky=type==SKY_LOCAL||type==SKY_SERVER;
			
			if(val==_groundGroup)
			{
				_isfirstGround=false;
				_isSky=false;
			}
			else
			{
				_isSky=true;
				//				 refreshNames();
			}
			initNames(currentType);
			initBattle(currentType);
			refreshAllBattle();
			refreshTime();
		}
		private function get checkInitFinish():Boolean
		{
			return Boolean(_mainMc);
		}
		private function initafterLodedAll():void
		{
			_currentGroup	=	_skyGroup;
			//			_mainMc.mouseChildren=false;
			_mainMc["skyTop"].mouseChildren=false;
			_mainMc["bottomTop"].mouseChildren=false;
			_mainMc["championIcon"].mouseEnabled=false;
			_mainMc["championIcon"].gotoAndStop(2);
			timeLine=1;
			//			timeTxt=1;
			setTurnInfo(0,0);
			attacktimeMc=1;
			refreshAllBattle();
			
		}
		private function refreshAllBattle():void
		{
			var len:int=currentGroup.length;
			for (var i:int = 1; i < len; i++) 
			{
				refreshIndexBattle(i);
			}
		}
		/**
		 *刷新单场战场的显示 
		 * @param index
		 * 
		 */		
		private function refreshIndexBattle(index:int):void
		{
			var battleNode:BattleNode=currentGroup[index];
			if(battleNode.result==BattleNode.T_WIN)
			{
				getTopBattleViewByIndex(index).gotoAndStop(2)
				getBotBattleViewByIndex(index).gotoAndStop(1)
			}
			else if(battleNode.result==BattleNode.B_WIN)
			{
				getBotBattleViewByIndex(index).gotoAndStop(2)
				getTopBattleViewByIndex(index).gotoAndStop(1)
			}
			else
			{
				getBotBattleViewByIndex(index).gotoAndStop(1)
				getTopBattleViewByIndex(index).gotoAndStop(1)
			}
			//			mc.mouseChildren=false;
			//			mc.buttonMode=true;
			_mainMc[CENTER_TIME_TXT].visible=true;
			if(index==31&&battleNode.result!=BattleNode.NONE)
			{
				var mc:MovieClip=	_mainMc["championIcon"];
				mc.gotoAndStop(1);
				var winer:String=battleNode.winer;
				mc["txt"].htmlText=winer;
				var currFrame:int=1;
				switch(currentType)
				{
					case SKY_LOCAL:
					case SKY_SERVER:
						currFrame = 1;
						break;
					case GROUND_LOCAL:
					case GROUND_SERVER:
						currFrame = 2;
						break;
				}
				mc["mc"].gotoAndStop(currFrame)
				_mainMc[CENTER_TIME_TXT].visible=false;
				if(!mc["nxclip"])
				{
					var nx:NxClip=new NxClip();
					mc["img"].addChild(nx);
					mc["nxclip"]=nx;
				}
				else
				{
					nx=mc["nxclip"];
				}
				var person:Object=_playerInfoMap[winer]
				if(person)
					nx.source=ResourcesPath.getHeadPic(person[ActorPropertyUI.MODEL],person[ActorPropertyUI.CLOTHE_MODEL])//battleNode.
				var mc1:MovieClip=getBattleIconByIndex(index);//
				mc1.gotoAndStop(1);
				var text:TextField=mc1["timetxt"] as TextField;
				text.htmlText=StaticIni.getSwfLangStr("Stride_15");
			}
			getBattleIconByIndex(index).gotoAndStop(battleNode.isSee);
			
		}
		private function getBattleIconByIndex(index:int):MovieClip
		{
			return _mainMc["icon_"+index];
		}
		private function getBotBattleViewByIndex(index:int):MovieClip
		{
			return _mainMc["line_"+index+"_b"]
		}
		private function getTopBattleViewByIndex(index:int):MovieClip
		{
			return _mainMc["line_"+index+"_t"]
		}
		public function set goldCoin( value :Number ):void
		{
			gold= value;
			if( value < 10000 )
			{
				_mainMc[MONEY_TXT].htmlText = value.toString();
			}
			else
			{
				_mainMc[MONEY_TXT].htmlText =Math.floor( value / 10000 )+StaticIni.getSwfLangStr("UIString_1000629");
			}
		}
		
		public function set magicCoin( value : Number ):void
		{
			magic = value;
			_mainMc[GOLD_TXT].text = value.toString();
		}
	}
}
/**
 *战场节点 
 * @author yanghr
 * 
 */
class BattleNode
{
	//	public static const BATTLE_NODE_GROUP:Vector.<battleNode>=new Vector.<battleNode>(32);
	/**
	 *榜单 常量 
	 */	
	public static const SKY:int=1,GROUND:int=2;
	/**
	 *战斗结果常量 
	 */	
	public static const NONE:int=0,T_WIN:int=1,B_WIN:int=2;
	
	private const RANGE:Array=[16,8,4,2,1];
	private var _order:int;
	private var t_player:String;
	
	private var b_player:String;
	/**
	 *第几场
	 */
	public function get order():int
	{
		return _order;
	}
	
	private var _result:int=NONE;
	
	/**
	 *战斗结果 
	 */
	public function get result():int
	{
		if(_result==NONE&&resulter)
		{
			initResult(resulter);
		}
		return _result;
	}
	
	
	
	public var resulter:String="";
	
	private var group:Vector.<BattleNode>;
	
	public function BattleNode():void
	{
	}
	public function init(group:Vector.<BattleNode>):void
	{
		_order=group.indexOf(this);
		//		this.result=result;
		this.group=group;
	}
	public function initPlayer(tplay:String,bPlayer:String):void
	{
		t_player=tplay;
		b_player=bPlayer;
	}
	public function initBattle(winer:String,isSee:int):void
	{
		initResult(winer);
		this.isSee=isSee;
	}
	private function initResult(winer:String):void
	{
		if(!winer)return ;
		if(t_Name==winer)
			_result=T_WIN;
		else if(b_Name==winer)
			_result=B_WIN;
		else
		{
			resulter=winer;
			trace("Error~~~BattleNode.initResult(winer)",winer,this);
		}
	}
	/**
	 *0 啥都没有, 1 战报, 2 下注 3 已下注
	 */		
	private const ICON_MAP:Object={0:4,1:1,2:2,3:3};
	private var _isSee:int;
	/**
	 *是否可以查看战报 
	 * 0 啥都没有, 1 战报, 2 下注 3已经下注
	 * @return 
	 * 
	 */	
	public function get isSee():int 
	{ 
		return ICON_MAP[_isSee]; 
	}
	
	public function set isSee(value:int):void
	{
		if (_isSee == value)
			return;
		_isSee = value;
	}
	
	private var _t_node:BattleNode;
	/**
	 *上节点 
	 * @return 
	 * 
	 */
	public function get t_node():BattleNode
	{
		if(!_t_node&&turnRest>0/*&&order>RANGE[0]*/)
			_t_node=group[turnBefore+turnRest*2-1]
		return _t_node;
	}
	
	
	private var _b_node:BattleNode;
	/**
	 *下节点 
	 * @return 
	 * 
	 */
	public function get b_node():BattleNode
	{
		if(!_b_node&&turnRest>0)
			_b_node=group[turnBefore+turnRest*2]
		return _b_node;
	}
	private function get turnBefore():int
	{
		var jj:int
		for (var i:int = 0; i < turnCount; i++) 
		{
			jj+=RANGE[i];
		}
		return jj;
	}
	private var _turnCount:int
	
	public function get turnCount():int
	{
		return _turnCount;
	}
	
	/**
	 *第几轮 
	 * @return 
	 * 
	 */	
	private function get turnRest():int
	{
		var order22:int=order
		for (var i:int = 0; i < RANGE.length; i++) 
		{
			if(order22>RANGE[i]&&order22-RANGE[i]<=RANGE[i+1])
			{
				_turnCount=i;
				return order22-RANGE[i]
			}
			order22=order22-RANGE[i];
		}
		return 0;
	}
	/**
	 *上节点名 
	 * @return 
	 * 
	 */	
	public function get t_Name():String
	{
		return t_player||(t_node?t_node.winer:"");
	}
	/**
	 *下节点名 
	 * @return 
	 * 
	 */	
	public function get b_Name():String
	{
		return b_player||(b_node?b_node.winer:"");
	}
	/**
	 *本来胜利者 
	 * @return 
	 * 
	 */	
	public function get winer():String
	{
		switch(result)
		{
			case T_WIN:
			{
				return t_Name
			}
			case B_WIN:
			{
				return b_Name
			}
				
			default:
			{
				return "";
			}
		}
	}
	/**
	 *清除结果 
	 * 
	 */	
	public function clear():void
	{
		_result=NONE;
	}
	public function toString():String
	{
		return ("tplayer:"+t_Name+"\tbPlayer:"+b_Name+"\tturnCount:"+turnRest+"\tOrder:"+order)
	}
}